<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib uri="/WEB-INF/tld/security.tld" prefix="s" %>

<fmt:setBundle basename="messages"/>

<script type="text/javascript" language="javascript">

	var validSubmit = true;
	
	function submitAddNewTransport(form){
		var costElementCode;
		var costElementDescription;
		
		if (validSubmit){
			costElementCode = document.getElementById('costElementCode').value;
			costElementDescription = document.getElementById('costElementDescription').value;
			
			document.getElementById('ceCode').value = costElementCode;
			document.getElementById('ceDescription').value = costElementDescription;
			
			validSubmit = false;
			
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitDeleteCostElementTransport(form, transportIndex){
		document.getElementById('transportIndex').value = transportIndex;

		var costElementCode;
		var costElementDescription;
		
		if (validSubmit){
			costElementCode = document.getElementById('costElementCode').value;
			costElementDescription = document.getElementById('costElementDescription').value;
			
			document.getElementById('costElementDelCode').value = costElementCode;
			document.getElementById('costElementDelDescription').value = costElementDescription;
			
			validSubmit = false;
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitSaveNewCostElement(form, flag){
		if (validSubmit){

			if (validateNewCostElementForm(form)){
				document.getElementById('createNewCostElement').value = flag;
				
				validSubmit = false;
				
				form.submit();
			}
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitCancel(form){
		if (validSubmit){
			validSubmit = false;
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function validateNewCostElementForm(form){
		var validCostElementForm = false;
		var costElementCode = "";
		var costElementDescription = "";
		
		costElementCode = document.getElementById('costElementCode').value;
		costElementDescription = document.getElementById('costElementDescription').value;
		
		costElementCode = trim(costElementCode);
		costElementDescription = trim(costElementDescription);
		
		if (costElementCode.length == 0){
			alert('<fmt:message key="warning.message.costelement.required.code"/>');
		} else if (costElementDescription == 0){
			alert('<fmt:message key="warning.message.costelement.required.description"/>');
		} else {
			validCostElementForm = true;
		}
		
		return validCostElementForm;
	}	
</script>
<table width="100%" align="center" border="0">
	<tr>
		<td style="width: 100%" align="center">
			<form name="NewCostElementForm" action="saveNewCostElement.do" method="POST">
				<input type="hidden" name="createNewCostElement" id="createNewCostElement"/>
				<table style="width: 340" align="center" border="0" cellspacing="1" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna">
							<fmt:message key="label.costelement.add.title"/>
						</th>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.costelement.list.code"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="costElementView.costElementCode" style="text-transform: uppercase;" id="costElementCode" value="<c:out value="${costElementView.costElementCode}"/>" onkeydown="return noPaste(event, this);" size="15" maxlength="5" onfocus="this.select();" class="campo_texto"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.costelement.list.description"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="costElementView.costElementDescription" id="costElementDescription" value="<c:out value="${costElementView.costElementDescription}"/>" onkeydown="return noPaste(event, this);" size="31" maxlength="50" onfocus="this.select();" class="campo_texto"/>
						</td>
					</tr>
				</table>				
			</form>
		</td>
	</tr>
	<tr>
		<td height="10">
		</td>
	</tr>
	<s:entidad nombreCortoMod="CATALOG" nombreCortoSub="COST" nombreCortoEnt="TRANLINK" permiso="ADMINISTRACION">
		<tr>
			<td width="100%">
				<form name="DeleteTransportForm" action="deleteCostElementTransport.do" method="POST">
					<input type="hidden" name="transportIndex" id="transportIndex"/>
					<input type="hidden" id="costElementDelCode" name="costElementView.costElementCode"/>
					<input type="hidden" id="costElementDelDescription" name="costElementView.costElementDescription"/>
					<table style="width: 340" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
						<tr>
							<td colspan="4" align="center" class="titulo_lista_interna">
								<b><fmt:message key="label.transports.query.transport"/></b>
							</td>
						</tr>
						<tr>
							<td class="titulocolumna" style="text-align: center; width: 10%">
								<a href="#>" onclick="submitAddNewTransport(TransportForm);">
									<img alt="<fmt:message key="common.add"/>" src="images/nuevo.gif" border="0">
								</a>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 40%">
								<fmt:message key="label.transports.query.transport"/>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 40%">
								<fmt:message key="label.transports.costelements.amount"/>
							</td>
							<td class="titulocolumna" style="text-align: center; width: 10%">
							</td>
						</tr>
						<c:forEach items="${costElementView.transports}" var="transport" varStatus="status">
							<tr class="<c:choose><c:when test="${status.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
								<td/>
								<td style="text-align: center; vertical-align: top">
									<c:out value="${transport.transportShortName}"/>
								</td>
								<td style="text-align: center; vertical-align: top">
									<fmt:formatNumber maxFractionDigits="4" pattern="###,###,##0.0000" value="${transport.transportCost}"/>
								</td>
								<td style="text-align: center; vertical-align: top;">
									<a href="#" onclick="submitDeleteCostElementTransport(DeleteTransportForm, '<c:out value="${status.index}"/>')">
										<img alt="<fmt:message key="common.delete"/>" src="images/eliminar.jpg" border="0">
									</a>
								</td>
							</tr>
						</c:forEach>
					</table>
				</form>
			</td>
		</tr>
	</s:entidad>
	<tr>
		<td width="100%">
			<table align="center" style="width: 340"  cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
				<tr>
					<td style="text-align: center; vertical-align: bottom;" class="columnadatoPar">
						&nbsp;
						<input type="button" value="<fmt:message key="label.transports.add.saveTransport"/>" onclick="submitSaveNewCostElement(NewCostElementForm, 0);" class="boton"/>
						<input type="button" value="<fmt:message key="label.transports.add.saveAndNewTransport"/>" onclick="submitSaveNewCostElement(NewCostElementForm, 1);" class="boton_large"/>
						&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="<fmt:message key="label.common.cancel"/>" onclick="submitCancel(CancelNewCostElementForm);" class="boton"/>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<form name="TransportForm" action="showAddCostElementTransport.do" method="POST">
	<input type="hidden" id="ceCode" name="costElementView.costElementCode"/>
	<input type="hidden" id="ceDescription" name="costElementView.costElementDescription"/>
</form>
<form name="CancelNewCostElementForm" action="costElementList.do" method="POST">
</form>